//
//  MYGHealthManagementReportCell.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/9.
//

import UIKit

class MYGHealthManagementReportCell: MYGHealthManagementBaseCell {
    var shareAction: ((MYGHealthManagementReportModel?) -> ())?
    var clickAction: ((MYGHealthManagementReportModel?) -> ())?
    // MARK: - Property
    static let kEndEditingNotificationKey = "kEndEditingNotificationKey"
    override var model: MYGHealthManagementRowModel? {
        didSet {
            guard let model = (model as? MYGHealthManagementReportModel) else { return }
            headImageView.sd_setImage(with: URL(string: model.headUrl), placeholderImage: UIImage(named: "head"))
            nameLabel.text = model.name
            sexIcon.image = model.sex == 0 ? UIImage(named: "Health/Management/woman") : UIImage(named: "Health/Management/man")
            timeLabel.text = model.time
            locationLabel.text = model.location
            numLabel.text = model.num
        }
    }

    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        contentView.backgroundColor = .white
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - UI
    private func configUI() {
        contentView.addSubview(cardView)
        cardView.addSubview(scrollView)
        scrollView.addSubview(scrollContentView)
        scrollContentView.addSubviews([
            headImageView,
            infoView,
            separator,
            timeLabel,
            locationLabel,
            numLabel,
            shareButton,
            button
        ])
        
        infoView.addSubviews([
            nameLabel,
            sexIcon
        ])
        
        cardView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalToSuperview().offset(15)
            make.bottom.equalToSuperview()
            make.height.equalTo(94)
        }
        
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(scrollView).offset(60)
            make.height.equalTo(scrollView)
        }
        
        headImageView.snp.makeConstraints { make in
            make.top.equalTo(20.5)
            make.left.equalTo(37)
            make.size.equalTo(CGSize(width: 31, height: 31))
        }
        
        infoView.snp.makeConstraints { make in
            make.centerX.equalTo(headImageView)
            make.left.greaterThanOrEqualTo(5)
            make.right.lessThanOrEqualTo(separator.snp.left).offset(-5)
            make.top.equalTo(headImageView.snp.bottom).offset(6.5)
            make.height.equalTo(18.5)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.top.left.bottom.equalToSuperview()
        }
        
        sexIcon.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
            make.left.equalTo(nameLabel.snp.right).offset(2)
            make.size.equalTo(CGSize(width: 8.5, height: 10.5))
        }
        
        separator.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(7)
            make.left.equalTo(headImageView.snp.right).offset(35.3)
            make.width.equalTo(0.5)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(separator.snp.right).offset(20)
            make.top.equalTo(15)
            make.height.equalTo(24)
            make.right.equalTo(shareButton.snp.left).offset(-8)
        }
        
        locationLabel.snp.makeConstraints { make in
            make.left.equalTo(separator.snp.right).offset(20)
            make.top.equalTo(timeLabel.snp.bottom).offset(4)
            make.height.equalTo(16.5)
            make.right.equalTo(shareButton.snp.left).offset(-8)
        }
        
        numLabel.snp.makeConstraints { make in
            make.left.equalTo(separator.snp.right).offset(20)
            make.top.equalTo(locationLabel.snp.bottom).offset(4)
            make.height.equalTo(16.5)
            make.right.equalTo(shareButton.snp.left).offset(-8)
        }
        
        shareButton.snp.makeConstraints { make in
            make.top.right.bottom.equalToSuperview()
            make.width.equalTo(60)
        }
        
        button.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.right.equalTo(shareButton.snp.left)
        }
        
//        contentView.addGestureRecognizer(scrollView.panGestureRecognizer)
    }
    
    // MARK: - lazy load
    
    private lazy var cardView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 5
        view.clipsToBounds = false
        view.layerShadowColor = .rgba(0, 0, 0, 0.1)
        view.layerShadowOffset = CGSize(width: 0, height: 0)
        view.layerShadowOpacity = 1
        view.layerShadowRadius = 5
        return view
    }()

    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = .white
        scrollView.layer.cornerRadius = 5
        scrollView.clipsToBounds = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.isUserInteractionEnabled = true
        scrollView.isScrollEnabled = false
        return scrollView
    }()
    
    private lazy var scrollContentView: UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var headImageView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "head"))
        imageV.layerCornerRadius = 15.5
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    
    private lazy var infoView: UIView = {
        let view = UIView()
        return view
    }()
    
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(44, 44, 44)
        label.font = .medium(13)
        return label
    }()
    
    private lazy var sexIcon: UIImageView = {
        let imageV = UIImageView()
        return imageV
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(17)
        label.textColor = .rgba(42, 42, 42)
        return label
    }()
    
    private lazy var locationLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(102, 102, 102)
        return label
    }()
    
    private lazy var numLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(102, 102, 102)
        return label
    }()
    
    private lazy var separator: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(240, 240, 240)
        return view
    }()
    
    
    private lazy var shareButton: UIButton = {
        let button = UIButton(type: .custom)
        button.bounds = CGRect(x: 0, y: 0, width: 60, height: 94)
        let gl = CAGradientLayer()
        gl.frame = CGRect(x: 0, y: 0, width: 60, height: 94)
        gl.locations = [0, 1]
        gl.colors = [UIColor.rgba(202, 172, 255, 1).cgColor, UIColor.rgba(143, 134, 255, 1).cgColor]
        gl.startPoint = CGPoint(x: 0, y: 0)
        gl.endPoint = CGPoint(x: 1, y: 1)
        gl.masksToBounds = true
        button.layer.addSublayer(gl)
        button.addTarget(self, action: #selector(shareEvent), for: .touchUpInside)
        button.setTitle("分享报告", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .regular(12)
        button.roundCorners(UIRectCorner(rawValue: UIRectCorner.topRight.rawValue | UIRectCorner.bottomRight.rawValue), radius: 5)
        return button
    }()
    
    
    private lazy var button: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(clickEvent), for: .touchUpInside)
        return button
    }()
}

extension MYGHealthManagementReportCell {
    @objc private func shareEvent() {
        shareAction?(self.model as? MYGHealthManagementReportModel)
    }
    
    @objc private func clickEvent() {
        clickAction?(self.model as? MYGHealthManagementReportModel)
    }
}


